package hzmg.akkacloud.server.core.handler;

import akka.actor.AbstractActor;
import akka.cluster.pubsub.DistributedPubSubMediator;
import com.hzmg.akkacloud.common.request.*;
import hzmg.akkacloud.server.remote.worker.WorkerClusterManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static hzmg.akkacloud.server.core.handler.WorkerRequestHandler.getWorkerRequestHandler;

/**
 * receive the server heartbeat for other registry.
 * @author zbw
 * @since 2022/9/19
 */
public class ClientHeartReceiveActor extends AbstractActor {
    Logger log= LoggerFactory.getLogger(ClientHeartReceiveActor.class);
    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(WorkerHeartbeat.class, this::onReceiveHeartbeatFromRegistry)
                .match(DistributedPubSubMediator.SubscribeAck.class, msg -> {})
                .matchAny(obj -> log.warn("[WorkerRequestAkkaHandler] receive unknown request: {}.", obj))
                .build();
    }
    /**
     * 接受其他注册中心传递的服务实例心跳请求
     *
     * @param hb 心跳包
     */
    private void onReceiveHeartbeatFromRegistry(WorkerHeartbeat hb) {
        //log.info("[HeartReceive]:receive the heartbeat from other registry.");
        WorkerClusterManagerService.updateStatus(hb);
    }
}
